package com.gxa.community.customer.util;

import com.alibaba.fastjson.JSONObject;
import com.gxa.community.core.utils.HttpComponentsClientRestfulHttpRequestFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Component
public class RangeUtils {
    /**
     * POST远程通信
     *
     * @param map MultiValueMap<T> 请求参数
     * @param url 请求地址
     * @return String 返回
     */
    public static String post(MultiValueMap map, String url) {
        RestTemplate template = new RestTemplate();
        HttpHeaders http = new HttpHeaders();
        http.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity<MultiValueMap<Object, Object>> r = new HttpEntity<>(map, http);
        String map1 = template.postForObject(url, r, String.class);
        return null;
    }

    /**
     * GET远程通信
     *
     * @param map MultiValueMap<T> 请求参数
     * @param url 请求地址
     * @return String 返回
     */
    public static String get(MultiValueMap map, String url) {
        RestTemplate template = new RestTemplate();
        HttpHeaders http = new HttpHeaders();
        http.add("Content-Type", "application/x-www-form-urlencoded");
        HttpEntity<MultiValueMap<Object, Object>> r = new HttpEntity(map, http);
        String map1 = template.getForObject(url, String.class, r);
        return map1;
    }

    public static String get(String url) {
        String s = RangeUtils.get(null, url);
        return s;
    }

    /**
     * GET远程通信
     *
     * @param url     URL地址
     * @param body    实体对象
     * @param headers 请求头
     * @param query   url链接参数
     * @return 返回结果
     */
    public static String get(String url, Object body, HttpHeaders headers, HashMap<String, String> query) {
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.setRequestFactory(new HttpComponentsClientRestfulHttpRequestFactory());

        int i = 0;
        StringBuilder urlBuilder = new StringBuilder(url);
        if (query != null)
            for (String s : query.keySet()) {
                if (i == 0) urlBuilder.append("?").append(s).append("=").append(query.get(s));
                else urlBuilder.append("&").append(s).append("=").append(query.get(s));
                i++;
            }
        url = urlBuilder.toString();
        if (headers == null)
            headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json");

        return restTemplate.exchange(url, HttpMethod.GET, new HttpEntity(JSONObject.toJSONString(body), headers), String.class).getBody();
    }

    /**
     * POST远程通信
     *
     * @param url     URL地址
     * @param body    实体对象
     * @param headers 请求头
     * @param query   url链接参数
     * @return 返回结果
     */
    public static String post(String url, Object body, HttpHeaders headers, HashMap<String, String> query) {
        RestTemplate restTemplate = new RestTemplate();

        int i = 0;
        StringBuilder urlBuilder = new StringBuilder(url);
        if (query != null)
            for (String s : query.keySet()) {
                if (i == 0) urlBuilder.append("?").append(s).append("=").append(query.get(s));
                else urlBuilder.append("&").append(s).append("=").append(query.get(s));
                i++;
            }
        url = urlBuilder.toString();
        if (headers == null)
            headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "application/json");

        return restTemplate.postForObject(url, new HttpEntity(JSONObject.toJSONString(body), headers), String.class);
    }

}
